package o2o.utils;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ToolUtils;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.system.Common;

import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

//订单的工具类
public class OrderUtils {
    private final static StringBuffer numbers = new StringBuffer("0123456789");

    private static Queue<String> orderSeriesQueue = new LinkedList<>();            // 生成队列订单
    private static Date orderDate = null;

    //随机生成订单号  年月日时分秒+随机数
    public synchronized static String getOrderNo() {
        if (orderDate == null || !DateUtils.isSameDate(orderDate, new Date())) {
            orderSeriesQueue.clear();
        }
        if (orderSeriesQueue.size() < 100) {
            // 再次生成9000个订单号
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 9000; i++) {
                sb.setLength(0);
                sb.append(ToolUtils.getSize0(i + 1, 4));
                orderSeriesQueue.offer(sb.toString());
            }
            orderDate = new Date();
        }
        String date = Common.sdfSeconds.format(new Date());

        return new StringBuffer(date).append(orderSeriesQueue.poll()).toString();
    }

//    //根据订单状态返回状态名称
//    public static String getOrdStatusName(int ordStatus) {
//        if (ordStatus == 0) {
//            return "取消订单";
//        } else if (ordStatus == 1) {
//            return "提交订单";
//        } else if (ordStatus == 2) {
//            return "已付款";
//        } else if (ordStatus == 3) {
//            return "确认接单";
//        } else if (ordStatus == 4) {
//            return "备货中";
//        } else if (ordStatus == 5) {
//            return "店家发货";
//        } else if (ordStatus == 9) {
//            return "订单完成";
//        } else if (ordStatus == 11) {
//            return "店家拒绝订单";
//        } else if (ordStatus == -1) {
//            return "申请退款";
//        } else if (ordStatus == -2) {
//            return "退款确认";
//        } else if (ordStatus == -9) {
//            return "完成退款";
//        } else if (ordStatus == -11) {
//            return "申请退货";
//        } else if (ordStatus == -12) {
//            return "确认退货";
//        } else if (ordStatus == -19) {
//            return "完成退货并退款";
//        }
//        return null;
//    }


    //翻译订单状体名称
    public static String getOrderStatusName(int orderStatus) {
        String orderStatusName = "";
        switch (orderStatus) {
            case ORDER_STATUS.SUBMIT: {
                orderStatusName = "订单已提交,等待支付";
            }
            break;
            case ORDER_STATUS.PAYED: {
                orderStatusName = "订单已支付成功";
            }
            break;
            case ORDER_STATUS.CANCEL: {
                orderStatusName = "订单未支付已取消";
            }
            break;
            case ORDER_STATUS.PAYED_CANCEL: {
                orderStatusName = "订单已支付已取消";
            }
            break;
            case ORDER_STATUS.PAYED_APPLY_CANCEL: {
                orderStatusName = "订单已支付，申请取消订单";
            }
            break;
            case ORDER_STATUS.CANCEL_BY_SYSTEM: {
                orderStatusName = "订单超时未支付，自动取消";
            }
            break;
            case ORDER_STATUS.SHOP_SURE: {
                orderStatusName = "商家已接单";
            }
            break;
            case ORDER_STATUS.SHOP_REFUSED: {
                orderStatusName = "商家已拒绝接单";
            }
            break;
            case ORDER_STATUS.SHOP_FINISH: {
                orderStatusName = "商家已确认消费，等待用户完成";
            }
            break;
            case ORDER_STATUS.SHOP_READY: {
                orderStatusName = "商家已完成备货，等待取货";
            }
            break;
            case ORDER_STATUS.SHOP_PUSH_GOODS: {
                orderStatusName = "商家已确认发货，请耐心等待";
            }
            break;
            case ORDER_STATUS.POSTMAN_DAODIAN: {
                orderStatusName = "配送员已到达取货位置，正在取货";
            }
            break;
            case ORDER_STATUS.POSTMAN_PULL_GOODS: {
                orderStatusName = "骑手已取货，请耐心等待";
            }
            break;
            case ORDER_STATUS.PEISONG: {
                orderStatusName = "正在配送，等待送达";
            }
            break;
            case ORDER_STATUS.POSTMAN_ZHIDING: {
                orderStatusName = "已指定配送员，等待配送员接单";
            }
            break;
            case ORDER_STATUS.POSTMAN_SURE: {
                orderStatusName = "骑手已接单，等待取货";
            }
            break;
            case ORDER_STATUS.POSTMAN_REFUSED: {
                orderStatusName = "骑手已拒绝接单";
            }
            break;
            case ORDER_STATUS.POSTMAN_FINISH: {
                orderStatusName = "骑手已确定送达，等待用户确认";
            }
            break;
            case ORDER_STATUS.POSTMAN_CANT_FINISH: {
                orderStatusName = "骑手无法送达";
            }
            break;
            case ORDER_STATUS.OVER: {
                orderStatusName = "订单已完成";
            }
            break;
        }

        return orderStatusName;
    }

    //翻译支付方式名称
//    public static String getPayTypeName(int orderStatus) {
//        String orderStatusName = "";
//        switch (orderStatus) {
//            case OrderInfo.payByNet: {
//                orderStatusName = "网上付款";
//            }
//            break;
//            case OrderInfo.payByArrive: {
//                orderStatusName = "货到付款";
//            }
//            break;
//            case OrderInfo.payByAli: {
//                orderStatusName = "支付宝支付";
//            }
//            break;
//            case OrderInfo.payByWX: {
//                orderStatusName = "微信支付";
//            }
//            break;
//        }
//        return orderStatusName;
//    }

    private static String getNo(long id, int size) {
        String idStr = String.valueOf(id);
        if (idStr.length() >= size) {
            return idStr;
        }
        return getSize0(size - idStr.length()) + idStr;
    }

    private static String getSize0(int size) {
        StringBuffer sb = new StringBuffer();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                sb.append("0");
            }
        }
        return sb.toString();
    }

    private static String getRandom(int size) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int range = numbers.length();
        for (int i = 0; i < size; i++) {
            sb.append(numbers.charAt(random.nextInt(range)));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getOrderNo());
    }
}
